/*
 * @Description: 文件上传工具类
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2023-10-25
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2023-11-10
 */

import { UPLOAD_FILETYPE_ENUM, UPLOAD_FILETYPE_IMAGE } from '@/enum/uploadEnum'
import env from '@/env/index'

/**
 * 获取文件前缀
 * @param fileName 文件名称
 * @returns
 */
export const getPrefix = (fileName: string) => (!fileName ? '' : fileName.substring(0, fileName.lastIndexOf('.')))

/**
 * 获取文件后缀
 * @param fileName 文件名称
 * @returns
 */
export const getSuffix = (fileName: string) => (!fileName ? '' : fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length))

/**
 * 获取文件类型
 * @param suffix 文件后缀
 * @returns
 */
export const getType = (suffix: string) => {
  if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].includes(suffix)) return UPLOAD_FILETYPE_ENUM.IMAGE
  if (['doc', 'docx', 'dot', 'dotx'].includes(suffix)) return UPLOAD_FILETYPE_ENUM.WORD
  if (['xls', 'xlsx', 'xlsm', 'csv'].includes(suffix)) return UPLOAD_FILETYPE_ENUM.EXCEL
  if (['ppt', 'pptx', 'pps', 'ppsx'].includes(suffix)) return UPLOAD_FILETYPE_ENUM.PPT
  if (['pdf'].includes(suffix)) return UPLOAD_FILETYPE_ENUM.PDF
  return UPLOAD_FILETYPE_ENUM.OTHER
}

/**
 * 获取文件类型图标
 * @param suffix 文件后缀
 * @returns
 */
export const getTypeImage = (suffix: string) => {
  if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].includes(suffix)) return UPLOAD_FILETYPE_IMAGE[UPLOAD_FILETYPE_ENUM.IMAGE]
  if (['doc', 'docx', 'dot', 'dotx'].includes(suffix)) return UPLOAD_FILETYPE_IMAGE[UPLOAD_FILETYPE_ENUM.WORD]
  if (['xls', 'xlsx', 'xlsm', 'csv'].includes(suffix)) return UPLOAD_FILETYPE_IMAGE[UPLOAD_FILETYPE_ENUM.EXCEL]
  if (['ppt', 'pptx', 'pps', 'ppsx'].includes(suffix)) return UPLOAD_FILETYPE_IMAGE[UPLOAD_FILETYPE_ENUM.PPT]
  if (['pdf'].includes(suffix)) return UPLOAD_FILETYPE_IMAGE[UPLOAD_FILETYPE_ENUM.PDF]
  return UPLOAD_FILETYPE_IMAGE[UPLOAD_FILETYPE_ENUM.EXCEL]
}

/**
 * 单文件上传
 * @param file 上传文件参数
 */
export const uploadSingleFile = (file: UploadTypes.IUploadSingleFile) => {
  const { maxSizeM = 10, success = () => {}, fail = () => {}, complete = () => {} } = file
  uni.chooseMessageFile({
    count: 1,
    success: (res) => {
      const { path, name, size } = res.tempFiles[0]
      const sizeM = Math.round((size / 1024 / 1024) * 100) / 100
      if (sizeM > maxSizeM) {
        uni.showToast({ icon: 'none', title: `文件大小不能超过${maxSizeM}M` })
        return
      }
      uni.showLoading({ title: '上传中...', mask: true })
      uni.uploadFile({
        url: env.filesUpload,
        filePath: path,
        name: 'files',
        success: (res) => {
          uni.hideLoading()
          uni.showToast({ icon: 'none', title: '上传成功' })
          const prefix = getPrefix(name),
            suffix = getSuffix(name),
            type = getType(suffix),
            typeImage = getTypeImage(suffix)
          const tempFileRes: UploadTypes.IUploadTempFile = { path, name, size, sizeM, prefix, suffix, type, typeImage }
          success(res, tempFileRes)
        },
        fail: (error) => {
          uni.hideLoading()
          uni.showToast({ icon: 'none', title: '上传失败' })
          fail(error)
        },
        complete: (res) => {
          complete(res)
        },
      })
    },
  })
}
